<template>
  <!-- 页面框架布局 -->
  <el-container class="home-container">
    <!-- 页面头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt />
        <span class="title">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut">退出</el-button>
    </el-header>
    <!-- 页面body区域 -->
    <el-container>
      <!-- 页面侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' :'200px'">
        <!-- 展开和折叠主菜单按钮 -->
        <div class="toggle-button" @click = "toggleCollaspe">|||</div>
        <!-- 侧边栏导航菜单 -->
        <el-menu
          default-active="1"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#439EFF"
          :collapse-transition = "false"
          unique-opened
          :collapse = "isCollapse"
          router
          :default-active = "activePath"
        
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for = "item in menuList" :key = "item.id">
            <!-- 一级菜单模版 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index = "'/'+subItem.path" v-for = "subItem in item.children" :key = "subItem.id" @click = "saveStateActivePath('/'+subItem.path)">
              <!-- 二级菜单模版 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 页面主内容区域 -->
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return {
      // 左侧菜单数据
     menuList : [],
     //一级菜单图标
     iconsObj : {
       '125' : 'iconfont icon-user',
       '103' : 'iconfont icon-quanxian',
       '101' : 'iconfont icon-shangping',
       '102' : 'iconfont icon-dingdan',
       '145' : 'iconfont icon-baobiao'
     },
     isCollapse:true,
     activePath:''
    }
  },
  methods: {
    //退出登录方法
    logOut() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    //获取左侧菜单数据的ajax请求方法
    async getListMenu(){
     const {data:res} = await this.$http.get('menus');
      if(res.meta.status !=200) return this.$message.err(res.meta.msg)
       this.menuList = res.data;
    },
    //展开和折叠主菜单按钮方法
    toggleCollaspe(){  
        
               this.isCollapse = !this.isCollapse
              
           
         
    },
    saveStateActivePath(activePath){
        //  console.log( window.sessionStorage.getItem('isCollapse'));
        // this.isCollapse = window.sessionStorage.getItem('isCollapse')
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath

    }
  },
   created(){
    //获取左侧菜单数据的ajax请求
    this.getListMenu();
    this.activePath = window.sessionStorage.getItem('activePath')
    if(window.sessionStorage.getItem('isCollapse')==null){
      console.log(111);
       window.sessionStorage.setItem('isCollapse',false);
    }else{
      this.isCollapse= !Boolean(window.sessionStorage.getItem('isCollapse'));
    }
   
 
  },
  components: {}
};
</script>

<style lang="less">
/*
   #373d41 aside
   #333744
   #eaedf1
   #4A5064
 */
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;

  .logo {
    display: flex;
    align-items: center;
    img {
      height: 100%;
    }
    .title {
      margin-left: 15px;
      font-size: 20px;
      color: #fff;
    }
  }
}
.el-aside {
  background-color: #333744;
  
  .el-menu{
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right:10px;
}
.toggle-button{
  padding: 3px 0;
  background-color: #4A5064;
  color:#fff;
  text-align: center;
  line-height: 20px;
  letter-spacing: 3px;
  font-size:13px;
  box-sizing: border-box;
}
</style>
